<template>
	<view>
		<view class="empty" v-if="show==false">
			<image
				src="https://gimg2.baidu.com/image_search/src=http%3A%2F%2Fpic.51yuansu.com%2Fpic3%2Fcover%2F01%2F82%2F40%2F596fa6dc00bb4_610.jpg&refer=http%3A%2F%2Fpic.51yuansu.com&app=2002&size=f9999,10000&q=a80&n=0&g=0n&fmt=jpeg?sec=1633499781&t=d37222e32213957ddbdd01d62e071309"
				mode="widthFix" style="width: 400rpx;"></image>
			<view class="empty-text">空空如也的购物</view>
			<view class="empty-button" @click="goshopping">去选购</view>
		</view>
		<view v-if="show==true">
			<view class="goods-detail" v-for="(item,index) in goods" :key="index">
				<view class="detail-left">
					<view class="goods-left">
						<checkbox-group @change="selected(item)">
							<label>
								<checkbox class="selected" color="#555555" :checked="checked" /><text></text>
							</label>
						</checkbox-group>
						<image :src="item.url" style="width: 94px;height: 94px;"></image>
					</view>
					<view class="size">
						<text style="font-size: 25rpx;">{{item.fname}}</text>
						<text class="goods-price">￥{{item.money}}总价</text>
					</view>
				</view>
				<view>
					<image src="../../static/x.png" @click="del(item)"
						style="width: 20px;height: 20px;margin-left: 50px;margin-bottom: 15px;"></image>
					<view class="detail-right">
						<text class="subtract" @click="reduce(item)">-</text>
						<text class="num">{{item.sum}}</text>
						<text @click="add(item)" class="add">+</text>
					</view>
				</view>

			</view>
			<view class="end">
				<view class="end-left">
					<checkbox-group @change="selectgoods()">
						<label>
							<checkbox :checked="allchecked" />全选
						</label>
					</checkbox-group>
					<view>
						总计：<text style="color: black;font-weight: bold;">￥ {{this.moneys}}</text>
					</view>
				</view>
				<view class="end-right">
					结算({{this.moneys}})
				</view>


			</view>
		</view>

	</view>
</template>

<script>
	export default {
		data() {
			return {
				show: true,
				allchecked: true,
				checked: true,
				state: false,
				moneys: '',
				goods: [
					// 	{
					// 	size:"女款-M",
					// 	num:1,
					// 	flag:true,
					// 	price:149,
					// 	goodsImage:"https://img0.baidu.com/it/u=4158246207,3235707994&fm=26&fmt=auto&gp=0.jpg",
					// },
					// {
					// 	size:"女款-xs",
					// 	num:1,
					// 	flag:true,
					// 	price:219,
					// 	goodsImage:"https://img0.baidu.com/it/u=811765333,1656843554&fm=11&fmt=auto&gp=0.jpg",
					// },
					// {
					// 	size:"女款-L",
					// 	num:1,
					// 	flag:true,
					// 	price:240,
					// 	goodsImage:"https://img1.baidu.com/it/u=233755383,2522308225&fm=26&fmt=auto&gp=0.jpg",
					// },
					// {
					// 	size:"女款-XXL",
					// 	num:1,
					// 	flag:true,
					// 	price:410,
					// 	goodsImage:"https://img0.baidu.com/it/u=3894000947,2570065196&fm=26&fmt=auto&gp=0.jpg",
					// },
					// {
					// 	size:"女款-XXL",
					// 	num:1,
					// 	flag:true,
					// 	price:500,
					// 	goodsImage:"https://img2.baidu.com/it/u=1001625387,3275765924&fm=26&fmt=auto&gp=0.jpg",
					// },
				],
			}
		},
		onLoad() {
			this.getHomeSlideFunc();
		},
		onShow() {
			this.getHomeSlideFunc()
		},
		methods: {
			goshopping() {
				uni.switchTab({
					url: '../index/index'
				})
			},
			del(item) {
				var _self = this;
				uni.request({
					url: 'https://www.xinyixjc.com/miniapp/api/deleteCatInfo',
					method: 'POST',
					data: {
						id: item.id
					},
					//post提交的时候加上这个header
					header: {
						'Content-Type': 'application/x-www-form-urlencoded',
						'X-Requested-With': 'xmlhttprequest'
					},
					success: (res) => {
						_self.getHomeSlideFunc();

					}
				});
			},
			getHomeSlideFunc() {
				var _self = this;
				uni.request({
					url: 'https://www.xinyixjc.com/miniapp/api/selectCat',
					method: 'POST',
					data: {
						userId: uni.getStorageSync('user').id
					},
					//post提交的时候加上这个header
					header: {
						'Content-Type': 'application/x-www-form-urlencoded',
						'X-Requested-With': 'xmlhttprequest'
					},
					success: (res) => {
						_self.goods = res.data.data.catInfos;
						_self.moneys = res.data.data.cat.totalPrice;
						console.log("金额" + _self.moneys);
						if (Object.keys(_self.goods).length === 0) {
							_self.show = false
						} else {
							_self.show = true
							console.log("用户数据" + _self.goods);
						}
					}
				});
			},
			change(e) {
				console.log(e + "是否选中")
			},

			selected(item) {
				this.state = !this.state;
				if (this.state) {
					this.addHomeSlideFunc(item.id, 3);
				} else {
					this.addHomeSlideFunc(item.id, 2);
				}
				item.flag = !item.flag
				if (!item.flag) {
					this.allchecked = false
					console.log("未选中");
				} else {
					const a = this.goods.forEach((item) => {
						console.log("选中");
						return item.flag === true
					})
					if (a) {
						this.allchecked = true
					} else {
						this.allchecked = false
					}
				}
			},
			selectgoods() {
				this.allchecked = !this.allchecked
				if (this.allchecked) {
					this.goods.map(item => {
						this.checked = true
						item.flag = true
						console.log("全选成功");
						this.selectedCat(1)
					})

				} else {
					this.checked = false
					this.goods.map(item => {
						item.flag = false
						console.log("全选失败");
						this.selectedCat(0)
					})
				}
			},
			reduce(item) {
				let num = item.sum
				if (num > 1) {
					num -= 1
				}
				item.sum = num
				this.addHomeSlideFunc(item.id, 1)
			},
			addHomeSlideFunc(id, type) {
				var _self = this;
				uni.request({
					url: 'https://www.xinyixjc.com/miniapp/api/reduceCatInfo',
					method: 'POST',
					data: {
						id: id,
						type: type
					},
					//post提交的时候加上这个header
					header: {
						'Content-Type': 'application/x-www-form-urlencoded',
						'X-Requested-With': 'xmlhttprequest'
					},
					success: (res) => {
						let str = '';
						if (type == 0) {
							str = '增加成功'
						} else {
							str = '减少成功'
						}
						if (res.data.status == 1001) {
							_self.getHomeSlideFunc();
							uni.showToast({
								title: str
							})

						}
					}
				});
			},
			selectedCat(type) {
				var _self = this;
				uni.request({
					url: 'https://www.xinyixjc.com/miniapp/api/selectedCat',
					method: 'POST',
					data: {
						userId: uni.getStorageSync('user').id,
						type: type
					},
					//post提交的时候加上这个header
					header: {
						'Content-Type': 'application/x-www-form-urlencoded',
						'X-Requested-With': 'xmlhttprequest'
					},
					success: (res) => {
						_self.getHomeSlideFunc()
					}
				});
			},
			add(item) {
				let num = item.sum
				item.sum = num + 1
				this.addHomeSlideFunc(item.id, 0)
			}
		},
		computed: {
			totalNum() {
				let totalNum = 0;
				this.goods.map(item => {
					item.flag ? totalNum += item.sum : totalNum += 0
				})
				return totalNum
			},
			totalPrice() {
				let totalPrice = 0;
				this.goods.map(item => {
					item.flag ? totalPrice += item.sum * item.money : totalPrice += 0
				})
				return totalPrice
			}
		}
	}
</script>

<style lang="scss">
	.goods {
		line-height: 80rpx;
		background-color: #FFFFFF;

		&-detail {
			display: flex;
			padding: 30rpx 15rpx 30rpx 30rpx;
			background-color: #fff;
			justify-content: space-between;
			border-bottom: 5rpx solid #F1F1F1;
			align-items: center;

			.detail-left {
				display: flex;

				.goods-left {
					display: flex;
					align-items: center;
				}
			}

			.size {
				display: flex;
				justify-content: space-around;
				flex-direction: column;
				margin-left: 30rpx;

				.goods-price {
					font-size: 25rpx;
					color: #F44545;

				}
			}

			.detail-right {
				text {
					width: 50rpx;
					line-height: 50rpx;
					text-align: center;
					display: inline-block;
					background-color: #F7F7F7;
					margin-right: 10rpx;
				}

				.add {
					color: #FA4305;
					border-radius: 10rpx 30rpx 30rpx 10rpx;
					margin-right: 20rpx;
				}

				.subtract {
					border-radius: 30rpx 10rpx 10rpx 30rpx;
				}
			}
		}
	}

	.empty {
		position: relative;
		text-align: center;
		display: flex;
		align-items: center;
		flex-direction: column;

		&-text {
			color: #808080;
			margin-bottom: 50rpx;
		}

		&-button {
			width: 300rpx;
			height: 90rpx;
			color: orange;
			border: 1rpx solid orange;
			text-align: center;
			line-height: 90rpx;
			border-radius: 48rpx;
		}
	}

	.end {
		width: 100%;
		height: 90rpx;
		background-color: #fff;
		position: fixed;
		bottom: 0px;
		position: absolute;
		left: 0;
		display: flex;
		align-items: center;

		&-left {
			width: 70%;
			display: flex;
			justify-content: space-between;
			padding: 0 30rpx;

			.end-flex {
				display: flex;
				align-items: center;
			}
		}

		&-right {
			width: 30%;
			line-height: 90rpx;
			background-color: black;
			text-align: center;
			color: #fff;
		}
	}
</style>
